package org.jwd.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author vizz
 * @since 2022/5/21 0:07
 *
 * 这个工具类可以解决 非bean继承beanFactoryAware ApplicationContextAware 无效的问题
 * TODO：上述非bean为啥会无效？
 */
@Slf4j
public class SpringContextUtils {

    private static ConfigurableApplicationContext context;

    public static ConfigurableApplicationContext getContext() {
        return context;
    }

    public static void setContext(ConfigurableApplicationContext context) {
        SpringContextUtils.context = context;
        log.info("context 初始化成功");
    }

    public static <T> T getBean(Class<T> requireType) {
        return context.getBean(requireType);
    }
}
